package com.biu.zone.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.biu.zone.R;
import com.biu.zone.application.ShotApp;
import com.biu.zone.common.utils.ImgUtil;
import com.biu.zone.common.utils.ScreenShotHelper;
import com.blankj.utilcode.util.ToastUtils;

/**
 * https://blog.csdn.net/u011068702/article/details/51387649
 * https://blog.csdn.net/leif_/article/details/50971659
 */
public class FloatingWindowService1 extends Service {

    private LinearLayout mFloatLayout = null;
    private WindowManager.LayoutParams wmParams = null;
    private WindowManager mWindowManager = null;
    private LayoutInflater inflater = null;
    private ImageButton mFloatView = null;

    private static final String TAG = "FloatingWindowService";


    @Override
    public void onCreate() {
        super.onCreate();

        createFloatView();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createFloatView() {
        wmParams = new WindowManager.LayoutParams();
        mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        inflater = LayoutInflater.from(getApplication());
        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
        mWindowManager.addView(mFloatLayout, wmParams);
        mFloatView = (ImageButton) mFloatLayout.findViewById(R.id.float_id);

        mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        mFloatView.setOnTouchListener(new View.OnTouchListener() {
            private int x;
            private int y;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = (int) event.getRawX();
                        y = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int moveX = (int) event.getRawX();
                        int moveY = (int) event.getRawY();
                        int diffX = moveX - x;
                        int diffY = moveY - y;
                        wmParams.x = wmParams.x + diffX;
                        wmParams.y = wmParams.y + diffY;

                        x = moveX;
                        y = moveY;

                        mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                        break;
                }
                return false;
            }
        });

        mFloatView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // hide the button
                mFloatView.setVisibility(View.INVISIBLE);

                Intent resultData = ((ShotApp) getApplication()).getIntent();
                int resultCode = ((ShotApp) getApplication()).getResult();
                ScreenShotHelper screenShotHelper = new ScreenShotHelper(getApplication(), resultCode, resultData, bitmap -> {
                    mFloatView.setVisibility(View.VISIBLE);
                    if (bitmap != null) {
                        ImgUtil.saveImageToGallery(getApplicationContext(), bitmap);
                    }
                    ToastUtils.showShort("截屏保存成功");
                });

                screenShotHelper.startScreenShot();

            }
        });

        Log.i(TAG, "created the float sphere view");
    }


}
